package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String CACHE_KEY_PREFIX = "NursingPlan:all";

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Transactional
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        List<NursingProjectPlan> list = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        List<NursingProjectPlanVo> list1 = list.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            nursingProjectPlanVo.setProjectId(String.valueOf(nursingProjectPlan.getProjectId()));
            nursingProjectPlanVo.setExecuteTime(nursingProjectPlan.getExecuteTime());
            nursingProjectPlanVo.setExecuteCycle(String.valueOf(nursingProjectPlan.getExecuteCycle()));
            nursingProjectPlanVo.setExecuteFrequency(Long.valueOf(nursingProjectPlan.getExecuteFrequency()));
            nursingProjectPlanVo.setPlanId(Long.valueOf(nursingProjectPlan.getPlanId()));
            nursingProjectPlanVo.setId(((nursingProjectPlan.getId())));
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        NursingPlan nursingPlan = getById(id);
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        nursingPlanVo.setProjectPlans(list1);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        this.save(nursingPlan);
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans)) {
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId());
            });
        }
        boolean b = nursingProjectPlanService.saveBatch(projectPlans);
        //删除redis缓存
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return b ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划DTO，包含护理计划的基本信息及关联的护理项目列表
     * @return 更新状态，1表示成功，0表示失败
     */
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        // 创建一个新的护理计划对象，并将DTO中的属性复制到该对象中
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);

        // 判断dto中的项目列表为空，如果不为空，则先删除护理计划与护理项目的关系，然后重新批量添加
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans)) {
            // 删除已有的护理计划与护理项目的关联关系
            nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
            // 为每个护理项目设置当前护理计划的ID
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId());
            });
        }
        // 批量保存护理项目，建立新的关联关系
        nursingProjectPlanService.saveBatch(projectPlans);
        boolean b = updateById(nursingPlan);
        //删除redis缓存
        redisTemplate.delete(CACHE_KEY_PREFIX);

        // 更新护理计划的基本信息，并返回更新状态
        return b ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     *
     * @param id 需要删除的护理计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNursingPlanById(Long id) {
        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery()
                .eq(NursingProjectPlan::getPlanId, id));
        boolean b = this.removeById(id);
        return b ? 1 : 0;
    }

    /**
     * 查询所有护理计划
     *
     * @return 所有护理计划列表
     */
    @Override
    public List<NursingPlanVo> listAll() {
        // 从缓存中获取所有护理计划
        List<NursingPlanVo> nursingPlanVos = (List<NursingPlanVo>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if (nursingPlanVos != null) {
            return nursingPlanVos;
        }
        // 没有缓存数据从数据库中查
        nursingPlanVos = nursingPlanMapper.listAll();
        // 将查询结果缓存起来
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, nursingPlanVos);
        return nursingPlanVos;
    }
}
